<?php

namespace app\common\logic;

class Data
{
    // public function getfirstchar($s0)
    // {
    //     $fchar = ord($s0{0});
    //     if ($fchar >= ord("A") and $fchar <= ord("z")) return strtoupper($s0{
    //         0});
    //     //$s1 = iconv("UTF-8","gb2312//IGNORE", $s0);
    //     // $s2 = iconv("gb2312","UTF-8//IGNORE", $s1);
    //     $s1 = $this->get_encoding($s0, 'GB2312');
    //     $s2 = $this->get_encoding($s1, 'UTF-8');
    //     if ($s2 == $s0) {
    //         $s = $s1;
    //     } else {
    //         $s = $s0;
    //     }
    //     $asc = ord($s{
    //         0}) * 256 + ord($s{
    //         1}) - 65536;
    //     // -24156
    //     if ($asc >= -20319 and $asc <= -20284) return "A";
    //     if ($asc >= -20283 and $asc <= -19776) return "B";
    //     if ($asc >= -19775 and $asc <= -19219) return "C";
    //     if ($asc >= -19218 and $asc <= -18711) return "D";
    //     if ($asc >= -18710 and $asc <= -18527) return "E";
    //     if ($asc >= -18526 and $asc <= -18240) return "F";
    //     if ($asc >= -18239 and $asc <= -17923) return "G";
    //     if ($asc >= -17922 and $asc <= -17418) return "I";
    //     if ($asc >= -17417 and $asc <= -16475) return "J";
    //     if ($asc >= -16474 and $asc <= -16213) return "K";
    //     if ($asc >= -16212 and $asc <= -15641) return "L";
    //     if ($asc >= -15640 and $asc <= -15166) return "M";
    //     if ($asc >= -15165 and $asc <= -14923) return "N";
    //     if ($asc >= -14922 and $asc <= -14915) return "O";
    //     if ($asc >= -14914 and $asc <= -14631) return "P";
    //     if ($asc >= -14630 and $asc <= -14150) return "Q";
    //     if ($asc >= -14149 and $asc <= -14091) return "R";
    //     if ($asc >= -14090 and $asc <= -13319) return "S";
    //     if ($asc >= -13318 and $asc <= -12839) return "T";
    //     if ($asc >= -12838 and $asc <= -12557) return "W";
    //     if ($asc >= -12556 and $asc <= -11848) return "X";
    //     if ($asc >= -11847 and $asc <= -11056) return "Y";
    //     if ($asc >= -11055 and $asc <= -10247) return "Z";
    //     return $asc;
    //     return null;
    // }
    public static function lunarToSolar($lunar)
    {
        $lunarYear = substr($lunar, 0, 4);
        $lunarMonth = substr($lunar, 5, 2);
        $lunarDay = substr($lunar, 8, 2);

        $a = array(1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200);
        $b = array(4, 10, 16, 22, 28, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105, 111, 117, 123, 129, 135, 141, 147, 153, 159, 165, 171, 177, 183, 189, 195, 201, 207, 213);
        $c = array(4, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175);
        $d = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31);
        $e = array(19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30);
        $f = array(18, 20, 22, 24, 26, 28, 30);
        $g = array(20);
        $h = array(21);
        $j = array(23);
        $k = array(24);
        $l = array(26);
        $m = array(28);
        $n = array(30);

        $year = $lunarYear - 1900;
        $month = $lunarMonth - 1;
        $day = $lunarDay - 1;

        $offset = intval((intval($year / 4) - intval($year / 100) + intval($year / 400)) * (intval($b[intval($month)] * $d[intval($day)])));

        if ($year == intval($year / 4) && $year != intval($year / 100) && $year != intval($year / 400)) {
            $offset += intval($c[intval($month)] * $d[intval($day)]);
        } elseif ($year == intval($year / 4) && $year == intval($year / 100) && $year != intval($year / 400)) {
            $offset += intval($c[intval($month)] * $d[intval($day)]) - intval($c[intval($month)] * $d[intval($day)]);
        } elseif ($year == intval($year / 4) && $year == intval($year / 100) && $year == intval($year / 400)) {
            $offset += intval($c[intval($month)] * $d[intval($day)]) + intval($c[intval($month)] * $d[intval($day)]);
        }

        if ($month == intval($month / 2)) {
            $offset += intval($e[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 1) {
            $offset += intval($f[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 2) {
            $offset += intval($g[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 3) {
            $offset += intval($h[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 4) {
            $offset += intval($j[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 5) {
            $offset += intval($k[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 6) {
            $offset += intval($l[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 7) {
            $offset += intval($m[intval($day)] * $d[intval($day)]);
        } elseif ($month == intval($month / 2) - 8) {
            $offset += intval($n[intval($day)] * $d[intval($day)]);
        }

        $solarYear = intval(intval($year + $offset / (intval($b[intval($month)] * $d[intval($day)]))));
        $solarMonth = intval(intval(((intval($offset % (intval($b[intval($month)] * $d[intval($day)])) + intval($b[intval($month)] * $d[intval($day)])) / (intval($d[intval($day)]))))));
        $solarDay = intval(intval(((intval($offset % (intval($b[intval($month)] * $d[intval($day)])) + intval($b[intval($month)] * $d[intval($day)])) % (intval($d[intval($day)]))))));

        if ($solarMonth == intval(intval(((intval($solarMonth - 1) * intval($d[intval($day)])) + intval($b[intval($month)] * $d[intval($day)])) / (intval($d[intval($day)]))))) {
            $solarMonth--;
        }

        if ($solarMonth < intval(intval(((intval(intval($solarMonth - 1) * intval($d[intval($day)])) + intval($b[intval($month)] * $d[intval($day)])) / (intval($d[intval($day)])))))) {
            $solarYear--;
        }
        return date("Y-m-d", mktime(0, 0, 0, $solarMonth + 1, $solarDay + 1, $solarYear));
    }
    public static function isValidUrl($url)
    {
        $pattern = "/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/";
        if (preg_match($pattern, $url)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * @name: get_encoding
     * @description: 自动检测内容编码进行转换
     * @param: string data
     * @param: string to  目标编码
     * @return: string
     **/
    public function get_encoding($data, $to)
    {
        $encode_arr = array('UTF-8', 'ASCII', 'GBK', 'GB2312', 'BIG5', 'JIS', 'eucjp-win', 'sjis-win', 'EUC-JP');
        $encoded = mb_detect_encoding($data, $encode_arr);
        $data = mb_convert_encoding($data, $to, $encoded);
        return $data;
    }
    public static function tree_to_list($tree, $child = 'child', $pk = 'id', $pid = 'parentId', $parent_id = 0, $parent = [])
    {
        if (empty($tree) || !is_array($tree)) {
            return $tree;
        }
        $arrRes = [];
        foreach ($tree as $k => $v) {
            $arrTmp = $v;
            unset($arrTmp[$child]);
            $arrTmp[$pid] = $parent_id;
            if ($parent_id == '0') {
                $arrTmp['p_doc_ids'] = $arrTmp[$pk];
            } else {
                $arrTmp['p_doc_ids'] = $parent['p_doc_ids'] . ',' . $arrTmp[$pk];
            }
            $arrRes[] = $arrTmp;
            if (!empty($v[$child])) {
                $arrTmp = self::tree_to_list($v[$child], $child, $pk, $pid, $arrTmp[$pk], $arrTmp);
                $arrRes = array_merge($arrRes, $arrTmp);
            }
        }
        return $arrRes;
    }
    // public function getPinyinSzm($zh)
    // {
    //     $ret = "";
    //     $s1 = iconv("UTF-8", "gb2312", $zh);
    //     $s2 = iconv("gb2312", "UTF-8", $s1);
    //     if ($s2 == $zh) {
    //         $zh = $s1;
    //     }
    //     for ($i = 0; $i < strlen($zh); $i++) {
    //         $s1 = substr($zh, $i, 1);
    //         $p = ord($s1);
    //         if ($p > 160) {
    //             $s2 = substr($zh, $i++, 2);
    //             $ret .= $this->getfirstchar($s2);
    //         } else {
    //             $ret .= $s1;
    //         }
    //     }
    //     return $ret;
    // }
    public static function listToTreeMulti($list, $root = 0, $pk = 'id', $pid = 'parentId', $child = 'child', $path = 'path', $lastpath = '')
    {
        $tree = array();
        foreach ($list as $v) {
            if ($v[$pid] == $root) {
                if (isset($v['filePath'])) {
                    $v[$path] = $lastpath . '/' . $v['filePath'];
                } else {
                    $v[$path] = '';
                }
                $v[$child] = self::listToTreeMulti($list, $v[$pk], $pk, $pid, $child, $path, $v[$path]);
                $tree[]    = $v;
            }
        }
        return $tree;
    }
    public static function filterEmptyKey($array, $key)
    {
        if (count($array) < 1) {
            return [];
        }
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                $array[$k] = self::filterEmptyKey($v, $key);
            }
            if (empty($array[$k]) && $k = $key) {
                unset($array[$k]);
            }
        }
        return $array;
    }
    /**
     * 一维数据数组生成数据树
     */
    public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
    {
        list($tree, $map) = [[], []];
        foreach ($list as $item) $map[$item[$id]] = $item;
        foreach ($list as $item) if (isset($item[$pid]) && isset($map[$item[$pid]])) {
            $map[$item[$pid]][$son][] = &$map[$item[$id]];
        } else $tree[] = &$map[$item[$id]];
        unset($map);
        return $tree;
    }

    /**
     * 一维数据数组生成数据树
     */
    public static function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
    {
        $tree = [];
        foreach (self::arr2tree($list, $id, $pid) as $attr) {
            $attr[$path] = "{$ppath}-{$attr[$id]}";
            $attr['sub'] = isset($attr['sub']) ? $attr['sub'] : [];
            $attr['spt'] = substr_count($ppath, '-');
            $attr['spl'] = str_repeat("　├　", $attr['spt']);
            $sub = $attr['sub'];
            unset($attr['sub']);
            $tree[] = $attr;
            if (!empty($sub)) $tree = array_merge($tree, self::arr2table($sub, $id, $pid, $path, $attr[$path]));
        }
        return $tree;
    }
    /**
     * 获取数据树子ID
     */
    public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid')
    {
        $ids = [intval($id)];
        foreach ($list as $vo) if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($id)) {
            $ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
        }
        return $ids;
    }
    public static function camelToUnder($str)
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . '_' . "$2", $str));
    }
}
